package com.wetao.module_user.presenter;

import android.content.Context;

import com.google.gson.Gson;
import com.wetao.lib_common.base.BaseListResponse;
import com.wetao.lib_common.base.BaseResponse;
import com.wetao.lib_common.bean.request.AddClassReq;
import com.wetao.lib_common.bean.response.AppVersionInfo;
import com.wetao.lib_common.bean.response.AreaSchoolInfo;
import com.wetao.lib_common.bean.response.CheckCodeInfo;
import com.wetao.lib_common.bean.response.Loginbean;
import com.wetao.lib_common.bean.response.Loginbean_CoachClass;
import com.wetao.lib_common.bean.response.SchoolClassAdminInfo;
import com.wetao.lib_common.bean.response.SchoolClassInfo;
import com.wetao.lib_common.bean.response.SchoolGradeInfo;
import com.wetao.lib_common.net.RetryWithDelay;
import com.wetao.lib_common.net.RxUtils;
import com.wetao.lib_common.net.ServerUtils;
import com.wetao.lib_common.net.callback.RequestCallback;
import com.wetao.lib_common.net.callback.RxErrorHandler;
import com.wetao.lib_common.utils.GsonUtil;
import com.wetao.lib_common.utils.JsonUtils;
import com.wetao.lib_common.utils.ToastUtil;
import com.wetao.module_user.contract.UserActivityContract;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import okhttp3.RequestBody;

/**
 * 登录 P
 */
public class UserActivityPresenter extends UserActivityContract.Presenter {

    private Context context;

    public UserActivityPresenter(Context context) {
        this.context = context;
    }

    @Override
    public void getAreaInfo(HashMap<String, Object> map, boolean isDialog, boolean cancelable) {
        if (getView()==null)return;
        String json = GsonUtil.GsonString(map);
        RequestBody requestBody =  RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"),json);
        ServerUtils.getCommonApi().queryAreaOrSchool(requestBody)
                .retryWhen(new RetryWithDelay(3,2))
                .compose(RxUtils.<BaseListResponse<AreaSchoolInfo>>bindToLifecycle(getView()))
                .compose(RxUtils.<BaseListResponse<AreaSchoolInfo>>getSchedulerTransformer())
                .subscribe(new RequestCallback<BaseListResponse<AreaSchoolInfo>>(context, RxErrorHandler.getInstance(),isDialog,cancelable) {
                    @Override
                    public void onNext(BaseListResponse<AreaSchoolInfo> baseResponse) {
                        super.onNext(baseResponse);
                        if(baseResponse.getCode() == 0){
                            getView().getAreaInfoResult(baseResponse);
                        }else {
                            getView().getAreaInfoError();
                            ToastUtil.initToast(baseResponse.getMsg());
                        }
                    }
                    @Override
                    public void onError(Throwable e) {
                        getView().getAreaInfoError();
                        super.onError(e);
                    }
                });
    }

    @Override
    public void getCheckCodeInfo(HashMap<String, Object> map, boolean isDialog, boolean cancelable) {
        if (getView()==null)return;
        String json = GsonUtil.GsonString(map);
        RequestBody requestBody =  RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"),json);
        ServerUtils.getCommonApi().sendMsg(requestBody)
                .retryWhen(new RetryWithDelay(3,2))
                .compose(RxUtils.<BaseResponse>bindToLifecycle(getView()))
                .compose(RxUtils.<BaseResponse>getSchedulerTransformer())
                .subscribe(new RequestCallback<BaseResponse>(context, RxErrorHandler.getInstance(),isDialog,cancelable) {
                    @Override
                    public void onNext(BaseResponse baseResponse) {
                        super.onNext(baseResponse);
                        if(baseResponse.getCode() == 0){
                            getView().getCheckCodeResult(baseResponse);
                        }else {
                            getView().getCheckCodeError();
                            //ToastUtil.initToast(baseResponse.getMsg());
                        }
                    }
                    @Override
                    public void onError(Throwable e) {
                        getView().getCheckCodeError();
                        super.onError(e);
                    }
                });
    }

    @Override
    public void getModifyPhoneInfo(HashMap<String, Object> map, boolean isDialog, boolean cancelable) {
        if (getView()==null)return;
        String json = GsonUtil.GsonString(map);
        RequestBody requestBody =  RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"),json);
        ServerUtils.getCommonApi().modifyMobile(requestBody)
                .retryWhen(new RetryWithDelay(3,2))
                .compose(RxUtils.<BaseResponse<Loginbean>>bindToLifecycle(getView()))
                .compose(RxUtils.<BaseResponse<Loginbean>>getSchedulerTransformer())
                .subscribe(new RequestCallback<BaseResponse<Loginbean>>(context, RxErrorHandler.getInstance(),isDialog,cancelable) {
                    @Override
                    public void onNext(BaseResponse<Loginbean> baseResponse) {
                        super.onNext(baseResponse);
                        if(baseResponse.getCode() == 0){
                            getView().getModifyPhoneResult(baseResponse);
                        }else {
                            getView().getModifyPhoneError();
                            ToastUtil.initToast(baseResponse.getMsg());
                        }
                    }
                    @Override
                    public void onError(Throwable e) {
                        getView().getModifyPhoneError();
                        super.onError(e);
                    }
                });
    }

    @Override
    public void getModifyPwdInfo(HashMap<String, Object> map, boolean isDialog, boolean cancelable) {
        if (getView()==null)return;
        String json = GsonUtil.GsonString(map);
        RequestBody requestBody =  RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"),json);
        ServerUtils.getCommonApi().modifyPassword(requestBody)
                .retryWhen(new RetryWithDelay(3,2))
                .compose(RxUtils.<BaseResponse>bindToLifecycle(getView()))
                .compose(RxUtils.<BaseResponse>getSchedulerTransformer())
                .subscribe(new RequestCallback<BaseResponse>(context, RxErrorHandler.getInstance(),isDialog,cancelable) {
                    @Override
                    public void onNext(BaseResponse baseResponse) {
                        super.onNext(baseResponse);
                        if(baseResponse.getCode() == 0){
                            getView().getModifyPwdResult(baseResponse);
                        }else {
                            getView().getModifyPwdError();
                            ToastUtil.initToast(baseResponse.getMsg());
                        }
                    }
                    @Override
                    public void onError(Throwable e) {
                        getView().getModifyPwdError();
                        super.onError(e);
                    }
                });
    }

    @Override
    public void getSectionGradeInfo(HashMap<String, Object> map, boolean isDialog, boolean cancelable) {
        if (getView()==null)return;
        String json = GsonUtil.GsonString(map);
        RequestBody requestBody =  RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"),json);
        ServerUtils.getCommonApi().querySchoolSectionGrade(requestBody)
                .retryWhen(new RetryWithDelay(3,2))
                .compose(RxUtils.<BaseListResponse<SchoolGradeInfo>>bindToLifecycle(getView()))
                .compose(RxUtils.<BaseListResponse<SchoolGradeInfo>>getSchedulerTransformer())
                .subscribe(new RequestCallback<BaseListResponse<SchoolGradeInfo>>(context, RxErrorHandler.getInstance(),isDialog,cancelable) {
                    @Override
                    public void onNext(BaseListResponse<SchoolGradeInfo> baseResponse) {
                        super.onNext(baseResponse);
                        if(baseResponse.getCode() == 0){
                            getView().getSectionGradeResult(baseResponse);
                        }else {
                            getView().getSectionGradeError();
                            ToastUtil.initToast(baseResponse.getMsg());
                        }
                    }
                    @Override
                    public void onError(Throwable e) {
                        getView().getSectionGradeError();
                        super.onError(e);
                    }
                });
    }

    @Override
    public void getClassAdminUpdateInfo(HashMap<String, Object> map, boolean isDialog, boolean cancelable) {
        if (getView()==null)return;
        String json = GsonUtil.GsonString(map);
        RequestBody requestBody =  RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"),json);
        ServerUtils.getCommonApi().joinClass(requestBody)
                .retryWhen(new RetryWithDelay(3,2))
                .compose(RxUtils.<BaseResponse<Loginbean>>bindToLifecycle(getView()))
                .compose(RxUtils.<BaseResponse<Loginbean>>getSchedulerTransformer())
                .subscribe(new RequestCallback<BaseResponse<Loginbean>>(context, RxErrorHandler.getInstance(),isDialog,cancelable) {
                    @Override
                    public void onNext(BaseResponse<Loginbean> baseResponse) {
                        super.onNext(baseResponse);
                        if(baseResponse.getCode() == 0){
                            getView().getClassAdminUpdateResult(baseResponse);
                        }else {
                            getView().getClassAdminUpdatError();
                            ToastUtil.initToast(baseResponse.getMsg());
                        }
                    }
                    @Override
                    public void onError(Throwable e) {
                        getView().getClassAdminUpdatError();
                        super.onError(e);
                    }
                });
    }

    @Override
    public void getClassCoachJoiInfo(HashMap<String, Object> map, boolean isDialog, boolean cancelable) {
        if (getView()==null)return;
        List<Object> list = new ArrayList(map.values());

        List<AddClassReq> list1= new ArrayList<AddClassReq>();
        list1.add(new AddClassReq((int)(list.get(0))));
        String json = GsonUtil.GsonString(list1);
        RequestBody requestBody =  RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"),json);
        ServerUtils.getCommonApi().insertChoiceClass(requestBody)
                .retryWhen(new RetryWithDelay(3,2))
                .compose(RxUtils.<BaseListResponse<Loginbean_CoachClass>>bindToLifecycle(getView()))
                .compose(RxUtils.<BaseListResponse<Loginbean_CoachClass>>getSchedulerTransformer())
                .subscribe(new RequestCallback<BaseListResponse<Loginbean_CoachClass>>(context, RxErrorHandler.getInstance(),isDialog,cancelable) {
                    @Override
                    public void onNext(BaseListResponse<Loginbean_CoachClass> baseResponse) {
                        super.onNext(baseResponse);
                        if(baseResponse.getCode() == 0){
                            getView().getClassCoachJoinResult(baseResponse);
                        }else {
                            getView().getClassCoachJoinError();
                            ToastUtil.initToast(baseResponse.getMsg());
                        }
                    }
                    @Override
                    public void onError(Throwable e) {
                        getView().getClassCoachJoinError();
                        super.onError(e);
                    }
                });
    }

    @Override
    public void getClassCoachUpdateInfo(HashMap<String, Object> map, boolean isDialog, boolean cancelable) {
        if (getView()==null)return;
        String json = GsonUtil.GsonString(map);
        RequestBody requestBody =  RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"),json);
        ServerUtils.getCommonApi().updateChoiceClass(requestBody)
                .retryWhen(new RetryWithDelay(3,2))
                .compose(RxUtils.<BaseListResponse<Loginbean_CoachClass>>bindToLifecycle(getView()))
                .compose(RxUtils.<BaseListResponse<Loginbean_CoachClass>>getSchedulerTransformer())
                .subscribe(new RequestCallback<BaseListResponse<Loginbean_CoachClass>>(context, RxErrorHandler.getInstance(),isDialog,cancelable) {
                    @Override
                    public void onNext(BaseListResponse<Loginbean_CoachClass> baseResponse) {
                        super.onNext(baseResponse);
                        if(baseResponse.getCode() == 0){
                            getView().getClassCoachUpdateResult(baseResponse);
                        }else {
                            getView().getClassCoachUpdateError();
                            ToastUtil.initToast(baseResponse.getMsg());
                        }
                    }
                    @Override
                    public void onError(Throwable e) {
                        getView().getClassCoachUpdateError();
                        super.onError(e);
                    }
                });
    }

    @Override
    public void getClassCoachExitInfo(HashMap<String, Object> map, boolean isDialog, boolean cancelable) {
        if (getView()==null)return;
        String json = GsonUtil.GsonString(map);
        RequestBody requestBody =  RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"),json);
        ServerUtils.getCommonApi().quitChoiceClass(requestBody)
                .retryWhen(new RetryWithDelay(3,2))
                .compose(RxUtils.<BaseListResponse<Loginbean_CoachClass>>bindToLifecycle(getView()))
                .compose(RxUtils.<BaseListResponse<Loginbean_CoachClass>>getSchedulerTransformer())
                .subscribe(new RequestCallback<BaseListResponse<Loginbean_CoachClass>>(context, RxErrorHandler.getInstance(),isDialog,cancelable) {
                    @Override
                    public void onNext(BaseListResponse<Loginbean_CoachClass> baseResponse) {
                        super.onNext(baseResponse);
                        if(baseResponse.getCode() == 0){
                            getView().getClassCoachExitResult(baseResponse);
                        }else {
                            getView().getClassCoachExitError();
                            ToastUtil.initToast(baseResponse.getMsg());
                        }
                    }
                    @Override
                    public void onError(Throwable e) {
                        getView().getClassCoachExitError();
                        super.onError(e);
                    }
                });
    }

    @Override
    public void getChoiceClassInfo(HashMap<String, Object> map, boolean isDialog, boolean cancelable) {
        if (getView()==null)return;
        String json = GsonUtil.GsonString(map);
        RequestBody requestBody =  RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"),json);
        ServerUtils.getCommonApi().getChoiceClass(requestBody)
                .retryWhen(new RetryWithDelay(3,2))
                .compose(RxUtils.<BaseListResponse<SchoolClassInfo>>bindToLifecycle(getView()))
                .compose(RxUtils.<BaseListResponse<SchoolClassInfo>>getSchedulerTransformer())
                .subscribe(new RequestCallback<BaseListResponse<SchoolClassInfo>>(context, RxErrorHandler.getInstance(),isDialog,cancelable) {
                    @Override
                    public void onNext(BaseListResponse<SchoolClassInfo> baseResponse) {
                        super.onNext(baseResponse);
                        if(baseResponse.getCode() == 0){
                            getView().getChoiceClassResult(baseResponse);
                        }else {
                            getView().getChoiceClassError();
                            ToastUtil.initToast(baseResponse.getMsg());
                        }
                    }
                    @Override
                    public void onError(Throwable e) {
                        getView().getChoiceClassError();
                        super.onError(e);
                    }
                });
    }

    @Override
    public void getAdminClassListInfo(HashMap<String, Object> map, boolean isDialog, boolean cancelable) {
        if (getView()==null)return;
        String json = GsonUtil.GsonString(map);
        RequestBody requestBody =  RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"),json);
        ServerUtils.getCommonApi().queryClassList(requestBody)
                .retryWhen(new RetryWithDelay(3,2))
                .compose(RxUtils.<BaseListResponse<SchoolClassInfo>>bindToLifecycle(getView()))
                .compose(RxUtils.<BaseListResponse<SchoolClassInfo>>getSchedulerTransformer())
                .subscribe(new RequestCallback<BaseListResponse<SchoolClassInfo>>(context, RxErrorHandler.getInstance(),isDialog,cancelable) {
                    @Override
                    public void onNext(BaseListResponse<SchoolClassInfo> baseResponse) {
                        super.onNext(baseResponse);
                        if(baseResponse.getCode() == 0){
                            getView().getAdminClassListResult(baseResponse);
                        }else {
                            getView().getAdminClassListError();
                            ToastUtil.initToast(baseResponse.getMsg());
                        }
                    }
                    @Override
                    public void onError(Throwable e) {
                        getView().getAdminClassListError();
                        super.onError(e);
                    }
                });
    }

    @Override
    public void getClientVersionInfo(HashMap<String, Object> map, boolean isDialog, boolean cancelable) {
        if (getView()==null)return;
        String json = (JsonUtils.getmGson()).toJson(map);
        RequestBody requestBody = RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"), json);
        ServerUtils.getCommonApi().getStudentScreenClientVersion(requestBody)
                .retryWhen(new RetryWithDelay(2, 1))
                .compose(RxUtils.<BaseResponse<AppVersionInfo>>bindToLifecycle(getView()))
                .compose(RxUtils.<BaseResponse<AppVersionInfo>>getSchedulerTransformer())
                .subscribe(new RequestCallback<BaseResponse<AppVersionInfo>>(context, RxErrorHandler.getInstance(), isDialog, cancelable) {
                    @Override
                    public void onNext(BaseResponse<AppVersionInfo> baseResponse) {
                        super.onNext(baseResponse);
                        if(baseResponse.getCode() == 0){
                            getView().getClientVersionResult(baseResponse);
                        }else {
                            getView().getClientVersionError();
                            ToastUtil.initToast(baseResponse.getMsg());
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        getView().getClientVersionError();
                        super.onError(e);
                    }
                });
    }

}
